package jmine.tec.hydra.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import jmine.tec.hydra.dao.HydraSpecificationDAO;
import jmine.tec.hydra.domain.specification.HydraSpecificationImpl;
import jmine.tec.persist.annotation.Alias;
import jmine.tec.persist.annotation.UniqueConstraint;
import bancosys.tec.component.Documentation;
import bancosys.tec.persist.bussobj.PersistableBusinessObject;
import bancosys.tec.persist.persister.annotation.NaturalKey;
import bancosys.tec.persist.persister.annotation.SystemId;

/**
 * Representa um sistema de integração.
 * 
 * @author piercio
 */
@Entity
@Table(name = "HYDRA_SYSTEM")
@Alias("HYDSYS")
@SequenceGenerator(name = "SEQ_HYDSYS", sequenceName = "SEQ_HYDSYS")
@Documentation("ARMAZENA OS SISTEMA DE INTEGRACAO.")
public class HydraSystem extends PersistableBusinessObject {

    private Long id;

    private String mnemonico;

    private String encoding;

    /**
     * Construtor para o Hibernate.
     */
    HydraSystem() {
        this(null);
    }

    /**
     * @param mnemonico mnemônico do sistema.
     */
    public HydraSystem(String mnemonico) {
        this.mnemonico = mnemonico;
    }

    /**
     * @return o id do sistema.
     */
    @Id
    @SystemId
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_HYDSYS")
    @Column(name = "COD_HYDRA_SYSTEM", nullable = false)
    @Documentation("CODIGO DO SISTEMA DE INTEGRACAO.")
    public Long getId() {
        return this.id;
    }

    /**
     * @param id o valor do id do sistema.
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return o mnemonico do sistema.
     */
    @NaturalKey
    @UniqueConstraint(name = "AK_HYDSYS_0")
    @Column(name = "MNE_HYDRA_SYSTEM", nullable = false)
    @Documentation("MNEMONICO DO SISTEMA.")
    public String getMnemonico() {
        return this.mnemonico;
    }

    /**
     * @param mnemonico o valor do mnemonico do sistema.
     */
    public void setMnemonico(String mnemonico) {
        this.mnemonico = mnemonico;
    }

    /**
     * @return o valor para o campo encoding
     */
    @Column(name = "ENCODING", nullable = false)
    @Documentation("ENCODING DO SISTEMA.")
    public String getEncoding() {
        return this.encoding;
    }

    /**
     * @param encoding o valor para o campo encoding
     */
    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return this.mnemonico;
    }

    /**
     * {@inheritDoc}
     */
    public void disable() {
        HydraSpecificationDAO specDao = super.getController().getDAOFactory().getDAOByClass(HydraSpecificationDAO.class);
        for (HydraSpecificationImpl spec : specDao.findBySystem(this)) {
            spec.disable();
            specDao.save(spec);
        }
    }

    /**
     * {@inheritDoc}
     */
    public void enable() {
        HydraSpecificationDAO specDao = super.getController().getDAOFactory().getDAOByClass(HydraSpecificationDAO.class);
        for (HydraSpecificationImpl spec : specDao.findBySystem(this)) {
            spec.enable();
            specDao.save(spec);
        }
    }
}
